package com.example.gitpart03;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.gitpart03.BookListAdapter;
import com.example.gitpart03.Book;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements BookListAdapter.OnItemClickListener {

    private RecyclerView recyclerView;
    private BookListAdapter adapter;
    private List<Book> bookList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化视图
        recyclerView = findViewById(R.id.recyclerViewBooks);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        // 初始化数据
        initBooks();

        // 设置适配器
        adapter = new BookListAdapter(this, bookList);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
    }

    private void initBooks() {
        bookList = new ArrayList<>();

        // 添加示例书籍数据
        bookList.add(new Book("1", "三国演义", "罗贯中",
                "https://picsum.photos/id/24/200/300",
                "《三国演义》是中国第一部长篇章回体历史演义小说...", 120));

        bookList.add(new Book("2", "红楼梦", "曹雪芹",
                "https://picsum.photos/id/25/200/300",
                "《红楼梦》，中国古典四大名著之首...", 120));

        bookList.add(new Book("3", "西游记", "吴承恩",
                "https://picsum.photos/id/26/200/300",
                "《西游记》是中国古代第一部浪漫主义章回体长篇神魔小说...", 100));

        bookList.add(new Book("4", "水浒传", "施耐庵",
                "https://picsum.photos/id/27/200/300",
                "《水浒传》，中国四大名著之一，是一部以宋朝为背景的长篇小说...", 120));
    }

    @Override
    public void onItemClick(Book book) {
        // 启动阅读Activity，传递书籍信息
        Intent intent = new Intent(this, BookReaderActivity.class);
        intent.putExtra("book", book);
        intent.putExtra("chapter", 1); // 默认从第一章开始
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            Toast.makeText(this, "设置功能将在未来版本中实现", Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}